<?php
defined('IN_LK') or die('Access Denied');
lk::lo_app('admin_common', 'admin', 0);
class role_manage extends admin_common
{
	/**
	 * model 句柄
	 */
	private $admin_db;
	private $admin_role_db;
	
	/**
	 * roles角色
	 */
	private $role;
	
	public function __construct()
	{
		parent::__construct();
		lk::lf_app('admin');
		lk::lo('form', '', 0);
		$this->admin_db = lk::load_model('admin');
		$this->admin_role_db = lk::load_model('admin_role');
		$this->role  = get_cache('admin_role');
	}
	
	/**
	 * 默认方法
	 */
	public function initialize()
	{
		$ac = isset($_GET['ac']) ? (int)$_GET['ac'] : 1;
		switch($ac)
		{
			case '1':
				$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
				$role_list = $this->admin_role_db->listinfo('*', '', $page, 25, '`listorder`,`roleid` ASC');
				$page_nav  = $this->admin_role_db->page_nav;
				break;
			case '2':
				break;
		}
		include self::admin_t('role_manage');
	}

	/**
	 * 成员管理
	 *
	 */
	public function member_manage()
	{
		$ac = 1;
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$roleid = isset($_GET['roleid']) ? (int)$_GET['roleid'] : 0;
		if(!$roleid || $roleid == 1)
		{
			showmsg(LANG('parameters_error'), false);
		}
		$admin_list = $this->admin_db->listinfo('*', "`roleid`='$roleid'", $page, 15);
		$page_nav   = $this->admin_db->page_nav;
		if($admin_list)
		{
			foreach($admin_list as $key => $admin)
			{
				$admin_list[$key]['rolename'] = $this->role[$admin['roleid']];
				$admin_list[$key]['lastlogintime'] = date('Y-m-d H:i:s', $admin['lastlogintime']);
			}
		}
		include self::admin_t('admin_manage');
	}

	/**
	 * 添加角色
	 */
	public function add_role()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			$role_fields = array('rolename', 'description', 'disabled', 'listorder');
			foreach($info as $key => $value)
			{
				if(!in_array($key, $role_fields))
				{
					unset($info[$key]);
				}
			}
			if($this->admin_role_db->insert($info))
			{
				$this->cache_role();
				showmsg(LANG('update_success'), true, '?m=admin&c=role_manage&ac=1');
			}
			else
			{
				showmsg(LANG('update_false'), false, '?m=admin&c=role_manage&ac=2');
			}
		}
		exit;
	}
	
	/**
	 * 编辑角色
	 */
	public function edit_role()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			if(!$info['roleid'] || $info['roleid'] == 1)
			{
				showmsg(LANG('parameters_error'), false);
			}
			if($info['disabled'] == 1)
			{
				$this->is_unchange($info['roleid']) ? showmsg(LANG('role_unchangeable'), false) : '';
			}
			$roleid = (int)$info['roleid'];
			$role_fields = array('rolename', 'description', 'disabled', 'listorder');
			$info = in_fields($info, $role_fields);
			$this->admin_role_db->update($info, "`roleid`='$roleid'");
			$this->cache_role();
			showmsg(LANG('update_success'), true, '?m=admin&c=role_manage&ac=1');
		}
		$roleid = isset($_GET['roleid']) ? (int)$_GET['roleid'] : 0;
		if(!$roleid || $roleid == 1)
		{
			showmsg(LANG('parameters_error'), false);
		}
		$info = $this->admin_role_db->get_one('*', "`roleid`='$roleid'");
		$ac = 3;
		$edit = 1;
		extract($info);
		include self::admin_t('role_manage');
	}
	
	/**
	 * 删除角色 ajx
	 * 需记录日志
	 */
	public function delete_role()
	{
		$roleid = isset($_GET['roleid']) ? (int)$_GET['roleid'] : exit('0');
		if($this->is_unchange($roleid))
		{
			exit('0');
		}
		if($this->admin_role_db->delete("`roleid`='$roleid'"))
		{
			$this->cache_role();
			exit('1');
		}
		exit('0');
	}
	
	/**
	 * 排序
	 */
	public function do_listorder()
	{
		if(isset($_POST['dosubmit']))
		{
			$listorder = $_POST['listorder'];
			foreach($listorder as $roleid => $order)
			{
				$roleid = (int)$roleid;
				$this->admin_role_db->update(array('listorder'=>(int)$order), "`roleid`='$roleid'");
			}
			showmsg(LANG('update_success'), true);
		}
		exit;
	}
	
	/**
	 * 改变状态
	 * 成功返回 1
	 */
	public function change_status()
	{
		$roleid = isset($_GET['roleid']) ? (int)$_GET['roleid'] : 0;
		$disabled = isset($_GET['disabled']) && $_GET['disabled'] ? 1 : 0;
		if($this->is_unchange($roleid))
		{
			exit('0');
		}
		if($this->admin_role_db->update(array('disabled'=>$disabled), "`roleid`='$roleid'"))
		{
			$this->cache_role();
			exit('1');
		}
		exit('0');
	}
	
	/**
	 * 角色缓存
	 * @access private
	 */
	private function cache_role()
	{
		$role = array();
		$infos = $this->admin_role_db->get('roleid,rolename', '`disabled`=0', '`listorder`,`roleid` ASC');
		foreach($infos as $info)
		{
			$role[$info['roleid']] = $info['rolename'];
		}
		set_cache('admin_role', $role);
		return $infos;
	}
	
	/**
	 * 禁止改变或者删除其下有成员的角色
	 * @param int $roleid 角色id
	 * @return boolean 是否可以删除或禁用 创始人或者角色下有成员 true 否则 false
	 */
	private function is_unchange($roleid)
	{
		$roleid = intval($roleid);
		if(!$roleid || $roleid == 1)
		{
			return true;
		}
		$r = $this->admin_db->get_one('userid', "`roleid`='$roleid'");
		return $r ? true : false;
	}
}